#!/usr/bin/ruby

require 'pp'
require 'json'
require 'optparse'
require 'bundler'
Bundler.require

require_relative 'application/db'
require_relative 'application/models/init'

options = {}
OptionParser.new do |opts|
    opts.banner = "Usage: serveradmin [options]"

    opts.on("-a", "--approve ID_USER", "approve a user by id.") do |m|
        DB.transaction do
            user = User.for_update.first(:id=>m)
            user.approve!
            puts "User #{user.email} successfully approved"
        end 
    end
    
    opts.on("-r", "--reject ID_USER", "reject a user by id.") do |m|
        DB.transaction do
            user = User.for_update.first(:id=>m)
            user.reject!
            puts "User #{user.email} successfully rejected"
        end 
    end

    opts.on("-l", "--list-unapproved", 
            "list all users that are registred but not approved yet.") do |m|
        DB[:user].where(:approved => false).all.each do |user|
            puts "id: #{user[:id]} | email: #{user[:email]}"        
        end
    end
end.parse!
